const fs = require("fs"); // 系统模块 无需安装 
const path = require("path"); // 系统模块 无需安装
const { addLog } = require("../utils");
const { baseDataPath,pathType } = require('../config');

function getData(projectName){
  let result = {};
  // 获取src目录下面data目录
  const data = fs.readdirSync(baseDataPath).filter(item=>item != '.DS_Store');
  if(projectName){
    const file = fs.readFileSync(`${baseDataPath+pathType+projectName}.json`, 'utf-8');
    result = JSON.parse(file);
  }else{
    // 获取src目录下面data目录下面的文件夹
    if(data.length > 0) {
      data.forEach((item, index) => {
        // 获取json文件内容
        const file = fs.readFileSync(`${baseDataPath+pathType+item}`, 'utf-8');
        // 转为json对象
        const json = JSON.parse(file);
        result[json.projectName] = json
      });
    }
  }
  return result 
}

function setData(projectName, data){
  // 写入文件
  fs.writeFileSync(`${baseDataPath+pathType+projectName}.json`, JSON.stringify( data));
}


module.exports = {
  setData,
  getData
};
